var start = document.getElementsByClassName('start')[0], //开始按钮
	time = document.getElementsByClassName('time')[0], // 倒计时
	title = document.getElementsByClassName('header')[0], //title
	wrap = document.getElementsByClassName('contain')[0], //最外层
	over = document.getElementsByClassName('over')[0], //结束
	restart = document.getElementsByClassName('restart')[0], //重新开始
	scodiv=document.getElementById('cursco'),//分数框
	bestsco=document.getElementById('best'),//最高分框
	level=document.getElementById('level'),//游戏难度等级框
	curscore=0,//分数
	scores=[],//历史分数 
	blo = null, //当前柱子
	lev=1,//困难等级
	blocks=[],
	rindex;//倒计时
var wrapHeight = window.getComputedStyle(wrap).height;
wrapHeight = wrapHeight.substring(0, wrapHeight.length - 2);
start.addEventListener('click', function() {
	//倒计时开始游戏
	time.style.display = 'block';
	rindex = 2;
	var sid = setInterval(function() {
		if(rindex === 0) {
			sid && clearInterval(sid);
			time.style.display = 'none';
			main();
		} else {
			time.style.backgroundImage = 'url(img/' + rindex + '.jpg)';
			rindex--;
		}
	}, 1000);
})

function main() {
	start.style.display = 'none'; //隐藏开始按钮
	title.style.display = 'none'; //隐藏title
	level.innerText=lev;//渲染困难等级
	/*加载鸟*/
	bird.show();
	bird.falldown();
	initBlock();
//	checkgame();
}

wrap.addEventListener('mousedown', function(eve) {
	bird.speed *= -1;
});
wrap.addEventListener('mouseup', function(eve) {
	bird.speed *= -1;
})
restart.addEventListener('click', function() {
	//重新开始
	over.style.display = 'none'; //隐藏游戏结束画面
	start.style.display = 'block'; //显示开始按钮
	title.style.display = 'block'; //显示title
	bird.div.style.display = 'none';
	document.getElementsByClassName('blockwrap')[0] && wrap.removeChild(blo.div);
	rindex = 3;
	bird.init();
	curscore=0;
	scodiv.innerText=curscore;
	setHightscore();
	
})

function end() {
	//游戏结束
	clearInterval(bird.downsid);
	over.style.display = 'block';
	clearInterval(blo.mid); //清除柱子移动
	clearInterval(bird.downsid);
	time.style.backgroundImage = 'url(img/' + 3 + '.jpg)';
	scores.push(curscore);

}
function rand(min,max){
	return (Math.random()*(max-min)+min) | 0;
}

function setHightscore(){
	//设置历史最高分
	var bst=0;
	console.log(scores);
	if(scores.length){
		bst=scores.sort(function(a,b){a-b})[0];
	}
	bestsco.innerText=bst;
	
}


function initBlock(){
	//生成block实例
	var hg1=rand(50,150);
	var hg2=rand(100,15*(10-lev));
	blo=new Block(hg1,hg2);
	blo.create();
	blo.move();
}
